package com.letv.android.client.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import com.ledim.bean.ChatRoomBean;
import com.ledim.bean.LedimChoiceCardBean;
import com.ledim.utils.ConstantUtil;
import com.ledim.utils.ToastUtil;
import com.letv.ads.util.LogInfo;
import com.letv.android.client.LetvSDK;
import com.letv.android.client.activity.AlbumPlayActivity;
import com.letv.android.client.activity.AlbumPlayRoomActivity;
import com.letv.android.young.client.R;
import com.letv.core.bean.AlbumNew;
import com.letv.core.bean.FloatBallBeanList;
import com.letv.core.bean.HomeMetaData;
import com.letv.core.constant.PlayConstant;
import com.letv.core.sysletvplayer.base.impl.LocalPlayerActivity;
import com.letv.core.utils.BaseTypeUtils;
import com.letv.core.utils.StatisticsUtils;
import com.letv.datastatistics.util.PageIdConstant;
import com.letv.http.LetvLogApiTool;
import java.util.ArrayList;

//import com.letv.download.manager.DownloadManager;

/**
 * 启动点播播放器工具类
 *
 * @author zhuqiao
 */
public class AlbumLaunchUtils {

    public static final String FLOAT_TOPIC_HALF_NINE = "9";
    private static final String LETVCLIENT = "letvclient";

    public static String BASE_PLAY_ACTIVITY_ACTION = "android.intent.action.BasePlayActivity";

    /**
     * 是否pid 入口
     * 1. 入口为vid：均记录本地缓存。以保证加载速度。
     * 2. 入口为pid：只记录4栏的缓存，不记录3栏的缓存。
     * 3. 无网时，进入已下载视频的播放页，需显示本地缓存，若无法命中，或缓存被清除，则显示“网络未连接”的提示。
     */
    public static boolean isPidIn = false;

    /**
     * 外跳浏览器
     */
    public static String Url = ""; // 来自外部浏览器的url
    public static String from = ""; // 来源

    public interface MLetvJumpProcess {

        int LT_MSITE_ACTION_TYPE_PLAY = 0; // 点播 pid vid

        int LT_MSITE_ACTION_TYPE_PAY = 1; // 支付 跳转到半屏播放器 pid vid

        int LT_MSITE_ACTION_TYPE_SHARE = 2; // 分享 跳转到播放器内分享 pid vid

        int LT_MSITE_ACTION_TYPE_LIVE = 3; // 直播 stream id

        int LT_MSITE_ACTION_TYPE_WEBVIEW = 4; // web view 页面 weburl

        int LT_MSITE_ACTION_TYPE_SUBJECT = 5;
        // 专题，如果特定专题的zid等为空则直接跳转到专题tab。web专题通过web方式调起

        int LT_MSITE_ACTION_TYPE_CHANNEL = 6; // 频道，cid可选

        int LT_MSITE_ACTION_TYPE_USERCENTER = 7;// 个人中心，我的tab

        int LT_MSITE_ACTION_TYPE_HOT = 8; // 热点tab

        int LT_MSITE_ACTION_TYPE_HALFPLAY = 9; // M站半屏

        int LT_MSITE_ACTION_TYPE_DOWNLOAD_COMPLETE_TAB = 1001; // 下载已完成标签

        int LT_MSITE_ACTION_TYPE_LOCALPLAY = 10;// 本地播放器

        int LT_MSITE_ACTION_TYPE_CONSUMERECORD = 11;// 消费记录

        int LT_MSITE_ACTION_TYPE_HOME = 12;// 首页

        int LT_MSITE_ACTION_TYPE_PLAY_RECORD = 13;// 播放记录

        int LT_MSITE_ACTION_TYPE_FAVORITE = 14;// 收藏
    }

    /**
     * 打开播放页
     * 播放地址，或者本地视频路径 全屏播放
     */
    public static void launch(Context context, String uriString, int playMode) {
        StatisticsUtils.mClickImageForPlayTime = System.currentTimeMillis();
        Intent intent = new Intent(context, AlbumPlayActivity.class);
        intent.putExtra(PlayConstant.LAUNCH_MODE, PlayConstant.LAUNCH_MODE_URI);
        intent.putExtra(PlayConstant.URI, uriString);
        intent.putExtra(PlayConstant.PLAY_MODE, playMode);
        if (playMode == PlayConstant.PLAY_MODE_SYSTEM) {
            intent.putExtra(PlayConstant.PAGE_ID, PageIdConstant.localPage);
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        LetvLogApiTool.createPlayLogInfo("LocalVideoClickPlayStart", "-", "uri=" + uriString);
        context.startActivity(intent);
    }

    /**
     * 打开4d播放页
     * 播放地址，或者本地视频路径 全屏播放
     */
    public static void launch(Context context, String videoUrl, String haptUrl, int playMode) {
        StatisticsUtils.mClickImageForPlayTime = System.currentTimeMillis();

        Intent intent = new Intent(context, AlbumPlayActivity.class);
        intent.putExtra(PlayConstant.LAUNCH_MODE, PlayConstant.LAUNCH_MODE_URI);
        intent.putExtra(PlayConstant.URI, videoUrl);
        intent.putExtra(PlayConstant.HAPT_URL, haptUrl);
        intent.putExtra(PlayConstant.PLAY_MODE, playMode);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        LetvLogApiTool.createPlayLogInfo("4DVideoClickPlayStart", "-", "uri=" + videoUrl);
        LogInfo.log("wlx", "4DVideoClickPlayStart   " + videoUrl);
        context.startActivity(intent);
    }

    /**
     * 打开播放页
     * 播放地址，或者本地视频路径 全屏播放
     */
    public static void launch(Context context, String uriString, int playMode, long seek) {
        StatisticsUtils.mClickImageForPlayTime = System.currentTimeMillis();
        Intent intent = new Intent(context, AlbumPlayActivity.class);
        intent.putExtra(PlayConstant.LAUNCH_MODE, PlayConstant.LAUNCH_MODE_URI);
        intent.putExtra(PlayConstant.URI, uriString);
        intent.putExtra(PlayConstant.SEEK, seek);
        intent.putExtra(PlayConstant.PLAY_MODE, playMode);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        LetvLogApiTool.createPlayLogInfo("LocalVideoClickPlayStart", "-", "uri=" + uriString);
        context.startActivity(intent);
    }

    /**
     * 打开播放页 UIControllerUtils或者 HalfPlayRelatedFragmentPlayerLibs 跳转而来
     * 播放专辑或者视频
     */
    public static void launch(Context context, long aid, long vid, int from,
                              LedimChoiceCardBean card) {
        launchForResult(context, aid, vid, from, true, card);
    }

    //搜索结果中的相关视频
    public static void launch(Context context, long aid, long vid, int from,
                              LedimChoiceCardBean card, boolean isLoadData) {
        launchForResult(context, aid, vid, from, true, card, isLoadData);
    }

    /**
     * 进入放映厅
     *
     * @param context
     * @param aid
     * @param vid
     */
    public static void launchRoom(Context context, long aid, long vid, int from,
                                  ChatRoomBean room) {
        Intent intent = new Intent(context, AlbumPlayRoomActivity.class);
        intent.putExtra(PlayConstant.LAUNCH_MODE, from);
        intent.putExtra(PlayConstant.AID, (int) aid);
        intent.putExtra(PlayConstant.VID, (int) vid);
        intent.putExtra(PlayConstant.FROM, from);
        intent.putExtra(PlayConstant.ROOM, room);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        LetvLogApiTool.createPlayLogInfo("VideoClickPlayStart", vid + " aid =" + aid, "-");
        
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
        ((Activity) context).overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

    public static void launchRoom(Context context, long aid, long vid, int from, ChatRoomBean room,
                                  boolean isMe, boolean isFromEditScreenRoom, boolean fromPushMsg) {
        Intent intent = new Intent(context, AlbumPlayRoomActivity.class);
        intent.putExtra(PlayConstant.LAUNCH_MODE, from);
        intent.putExtra(PlayConstant.AID, (int) aid);
        intent.putExtra(PlayConstant.VID, (int) vid);
        intent.putExtra(PlayConstant.FROM, from);
        intent.putExtra(ConstantUtil.FROM_PUSH_MSG, fromPushMsg);
        intent.putExtra(PlayConstant.ROOM, room);
        intent.putExtra(PlayConstant.IS_ME, isMe);
        intent.putExtra(PlayConstant.IS_FROM_EDIT_SCREENROOM, isFromEditScreenRoom);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        LetvLogApiTool.createPlayLogInfo("VideoClickPlayStart", vid + " aid =" + aid, "-");

        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
        ((Activity) context).overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }
    public static void launchRoom(Context context, long aid, long vid, int from, ChatRoomBean room,
                                  boolean isMe, boolean isFromEditScreenRoom) {
        Intent intent = new Intent(context, AlbumPlayRoomActivity.class);
        intent.putExtra(PlayConstant.LAUNCH_MODE, from);
        intent.putExtra(PlayConstant.AID, (int) aid);
        intent.putExtra(PlayConstant.VID, (int) vid);
        intent.putExtra(PlayConstant.FROM, from);
        intent.putExtra(PlayConstant.ROOM, room);
        intent.putExtra(PlayConstant.IS_ME, isMe);
        intent.putExtra(PlayConstant.IS_FROM_EDIT_SCREENROOM, isFromEditScreenRoom);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        LetvLogApiTool.createPlayLogInfo("VideoClickPlayStart", vid + " aid =" + aid, "-");

        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
        ((Activity) context).overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

    public static void launchForNoResult(Context context, long aid, long vid, int from) {
        launchForResult(context, aid, vid, from, false, null);
    }

    private static void launchForResult(Context context, long aid, long vid, int from,
                                        boolean forResult, LedimChoiceCardBean card) {
        StatisticsUtils.mClickImageForPlayTime = System.currentTimeMillis();
        //        int launchMode = 0;
        //        if (PlayConstant.LAUNCH_FROM_MSITE != from) {
        //            launchMode = PlayConstant.LAUNCH_MODE_ALBUM;
        //        } else {
        //            launchMode = PlayConstant.LAUNCH_MODE_VIDEO;
        //        }
        Intent intent = new Intent(context, AlbumPlayActivity.class);
        intent.putExtra(PlayConstant.LAUNCH_MODE, from);
        intent.putExtra(PlayConstant.AID, (int) aid);
        intent.putExtra(PlayConstant.VID, (int) vid);
        intent.putExtra(PlayConstant.FROM, from);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        LetvLogApiTool.createPlayLogInfo("VideoClickPlayStart", vid + " aid =" + aid, "-");
        intent.putExtra(PlayConstant.CARD, card);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
        ((Activity) context).overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

    //搜索结果中的相关视频
    private static void launchForResult(Context context, long aid, long vid, int from,
                                        boolean forResult, LedimChoiceCardBean card,
                                        boolean isLoadData) {
        StatisticsUtils.mClickImageForPlayTime = System.currentTimeMillis();
        Intent intent = new Intent(context, AlbumPlayActivity.class);
        intent.putExtra(PlayConstant.LAUNCH_MODE, from);
        intent.putExtra(PlayConstant.AID, (int) aid);
        intent.putExtra(PlayConstant.VID, (int) vid);
        intent.putExtra(PlayConstant.IS_LOAD_DATA, isLoadData);
        intent.putExtra(PlayConstant.FROM, from);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        LetvLogApiTool.createPlayLogInfo("VideoClickPlayStart", vid + " aid =" + aid, "-");
        intent.putExtra(PlayConstant.CARD, card);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
        ((Activity) context).overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

    /**
     * 打开播放页(从二维码扫描进入)
     * 播放专辑或者视频
     */
    public static void launch(Context context, long aid, long vid, long htime, String ref,
                              String type, int from) {
        StatisticsUtils.mClickImageForPlayTime = System.currentTimeMillis();
        int launchMode = PlayConstant.LAUNCH_MODE_ALBUM;
        Intent intent = new Intent(context, AlbumPlayActivity.class);
        intent.putExtra(PlayConstant.LAUNCH_MODE, launchMode);
        intent.putExtra(PlayConstant.AID, (int) aid);
        intent.putExtra(PlayConstant.VID, (int) vid);
        intent.putExtra(PlayConstant.HTIME, htime);
        intent.putExtra(PlayConstant.REF, ref);
        intent.putExtra(PlayConstant.TYPE, type);
        intent.putExtra(PlayConstant.FROM, from);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        LetvLogApiTool.createPlayLogInfo("VideoClickPlayStart", vid + " aid =" + aid, "-");
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
        ((Activity) context).overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

    /**
     * 打开播放页
     * 播放连播专辑或视频
     */
    public static void launch(Context context, long aid, long vid, int from,
                              ArrayList<HomeMetaData> videoList, int recommendIndex) {
        StatisticsUtils.mClickImageForPlayTime = System.currentTimeMillis();
        int launchMode = PlayConstant.LAUNCH_MODE_ALBUM;
        Intent intent = new Intent(context, AlbumPlayActivity.class);
        intent.putExtra(PlayConstant.LAUNCH_MODE, launchMode);
        intent.putExtra(PlayConstant.AID, (int) aid);
        intent.putExtra(PlayConstant.VID, (int) vid);
        intent.putExtra(PlayConstant.FROM, from);
        intent.putExtra(PlayConstant.VIDEO_LIST, videoList);
        LogInfo.log("wxf", "======launch, recommendIndex" + recommendIndex);
        intent.putExtra(PlayConstant.RECOMMEND_INDEX, recommendIndex);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        /**
         * 解决bug:连续播放点击回退键没有退出播放器而直接返回到上一个专辑的页面
         * libo
         */
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        LetvLogApiTool.createPlayLogInfo("RecommendVideoClickPlayStart", vid + " aid =" + aid, "-");
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
        ((Activity) context).overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

    /**
     * 打开播放页
     * 播放专辑或者视频(播放记录专用)
     */
    public static void launch(Context context, long aid, long vid, int from,
                              boolean checkVideoDownloaded) {
        StatisticsUtils.mClickImageForPlayTime = System.currentTimeMillis();
        int launchMode = PlayConstant.LAUNCH_MODE_ALBUM;
        if (checkVideoDownloaded) {// 在清除应用数据的无网条件下，判断视频是否已下载
            /**
             * 不管是无网还是有网的模式下都需要判断此视频是否为本地视频，优先播放本地视频
             * 去掉 !LetvToolsPlayerLibs.hasNet()网络环境的判断。
             *
             */
            if (vid > 0) {
                //                if (null != DownloadManager.getLocalVideoBean(vid)) {
                //                    launchMode = PlayConstant.LAUNCH_MODE_DOWNLOAD;
                //                }
            }
        }
        Intent intent = new Intent(context, AlbumPlayActivity.class);
        intent.putExtra(PlayConstant.LAUNCH_MODE, launchMode);
        intent.putExtra(PlayConstant.AID, (int) aid);
        intent.putExtra(PlayConstant.VID, (int) vid);
        intent.putExtra(PlayConstant.FROM, from);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        LetvLogApiTool.createPlayLogInfo("FromRecordVideoClickPlayStart",
                vid + " aid =" + aid, "-");
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
        ((Activity) context).overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

    /**
     * 打开播放页
     * 播放专辑或者视频
     */
    public static void launch(Context context, long aid, long vid, int from, long seek) {
        StatisticsUtils.mClickImageForPlayTime = System.currentTimeMillis();
        int launchMode = PlayConstant.LAUNCH_MODE_ALBUM;
        Intent intent = new Intent(context, AlbumPlayActivity.class);
        intent.putExtra(PlayConstant.LAUNCH_MODE, launchMode);
        intent.putExtra(PlayConstant.AID, (int) aid);
        intent.putExtra(PlayConstant.VID, (int) vid);
        intent.putExtra(PlayConstant.FROM, from);
        intent.putExtra(PlayConstant.SEEK, seek);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        LetvLogApiTool.createPlayLogInfo("VideoClickPlayStart", vid + " aid =" + aid, "-");
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    /**
     * 打开播放页
     * 播放专辑或者视频(杜比专用)
     */
    public static void launch(Context context, long aid, long vid, boolean isDolby, int from) {
        StatisticsUtils.mClickImageForPlayTime = System.currentTimeMillis();
        int launchMode = 0;
        if (!isDolby) {
            launchMode = PlayConstant.LAUNCH_MODE_ALBUM;
        } else {
            launchMode = PlayConstant.LAUNCH_MODE_VIDEO;
            AlbumPlayActivity.sIsFromDobiChannel = true;
        }
        Intent intent = new Intent(context, AlbumPlayActivity.class);
        intent.putExtra(PlayConstant.LAUNCH_MODE, launchMode);
        intent.putExtra(PlayConstant.AID, (int) aid);
        intent.putExtra(PlayConstant.VID, (int) vid);
        intent.putExtra(PlayConstant.IS_DOLBY, isDolby);
        intent.putExtra(PlayConstant.FROM, from);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        LetvLogApiTool.createPlayLogInfo("DolbyVideoClickPlayStart", vid + " aid =" + aid, "-");
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    /**
     * 焦点图点击入口BasePlayActivity.launchLives(context, null, pid, vid, true);只有streamId和url,也有防盗链加密
     *
     * @param context
     * @param code
     * @param streamId
     *         这个streamId一定要和url中的streamId一致，不然会导致加密防盗链参数错误
     * @param url
     * @param full
     */
    public static void launchLives(Context context, String code, String streamId, String url,
                                   boolean full, int from, String programName) {
        // BasePlayActivityPlayerLibs.clickImageForPlayPageTime = System.currentTimeMillis();
        // BasePlayActivityPlayerLibs.clickImageForPlayPageTime2 =
        // LetvUtilPlayerLibs.timeClockString("yyyyMMdd_HH:mm:ss");
        LogInfo.log("wuxinrong", "---------launchLives");
        Intent intent = new Intent();
        intent.setAction(BASE_PLAY_ACTIVITY_ACTION);
        if ("sports".equals(code)) {
            intent.putExtra("launchMode", PlayConstant.LAUNCH_MODE_LIVE_SPORTS);
        } else if ("ent".equals(code)) {
            intent.putExtra("launchMode", PlayConstant.LAUNCH_MODE_LIVE_ENTERTAINMENT);
        } else if ("music".equals(code)) {
            intent.putExtra("launchMode", PlayConstant.LAUNCH_MODE_LIVE_MUSIC);
        } else if ("other".equals(code)) {
            intent.putExtra("launchMode", PlayConstant.LAUNCH_MODE_LIVE_OTHER);
        } else {
            intent.putExtra("launchMode", PlayConstant.LAUNCH_MODE_LIVE_URL);
        }
        LogInfo.log("live",
                "launchLives code = " + code + " , streamId = " + streamId + " , url = " + url +
                        " , full = " + full);
        intent.putExtra("from", from);
        intent.putExtra(PlayConstant.FLOATPAGEINDEX, FloatBallBeanList.FLOAT_LIVE_CATETROY_EIGHT);
        intent.putExtra(PlayConstant.FLOATINDEX, code);
        intent.putExtra(PlayConstant.LIVE_CODE, code);
        intent.putExtra(PlayConstant.LIVE_STREAMID, streamId);
        intent.putExtra(PlayConstant.LIVE_URL, url);
        intent.putExtra(PlayConstant.LIVE_FULL_ONLY, full);
        if (!TextUtils.isEmpty(programName)) {
            intent.putExtra(PlayConstant.LIVE_PROGRAM_NAME, programName);
        }

        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        LetvLogApiTool.createPlayLogInfo("LiveVideoClickPlayStart", "-",
                "streamId=" + streamId + " full=" + full + " url=" + url);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    /**
     * 全屏指定高低码流
     *
     * @param context
     * @param zhiboId
     * @param selectLowHigh 是否指定播放的高低码流，指定为true，不指定为false
     * @param isLow true 低码流 false 高码流
     * @param from wxf 增加传入参数from.解决小窗返回半屏，再返回不跳首页的问题。原来调用此方法没有from传入参数改为-1
     */
    //    public static void launchLiveById(Context context, String zhiboId, boolean selectLowHigh, boolean isLow, int from) {
    //        LogInfo.log("LetvHttp", "ActivityLauncher launchLiveById zhiboId = " + zhiboId + " selectLowHigh = "
    //                + selectLowHigh + " , isLow = " + isLow);
    //
    //        if (!NetworkUtils.isNetworkAvailable(context)) {
    //            UIsUtils.showToast(context, context.getString(R.string.letv_net_no));
    //            return;
    //        }
    //
    //        Intent intent = new Intent();
    //        intent.setAction(BASE_PLAY_ACTIVITY_ACTION);
    //        intent.putExtra("launchMode", PlayConstant.LAUNCH_MODE_LIVE_ID);
    //        intent.putExtra(PlayConstant.LIVE_LAUNCH_ID, zhiboId);
    //        intent.putExtra(PlayConstant.LIVE_FULL_ONLY, true); // 全屏
    //        intent.putExtra("from", from);
    //        if (selectLowHigh) {
    //            intent.putExtra(PlayConstant.LIVE_IS_LOW, isLow);
    //        }
    //        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    //        LetvLogApiTool.createPlayLogInfo("LivePushVideoClickPlayStart", "-", "channel_ename=" + zhiboId
    //                + " , context instanceof Activity " + (context instanceof Activity));
    //        if (!(context instanceof Activity)) {
    //            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    //        }
    //        context.startActivity(intent);
    //    }

    /**
     * 打开播放页
     *
     * 播放视频(H265)
     *
     * added by chenyg
     * */
    //    public static void launchH265(Context context, long aid, long vid, int type, int from) {
    //        StatisticsUtils.mClickImageForPlayTime = System.currentTimeMillis();
    //        int launchMode = PlayConstant.LAUNCH_MODE_H265_ALBUM;
    //        Intent intent = new Intent(context, AlbumPlayActivity.class);
    //        intent.putExtra(PlayConstant.LAUNCH_MODE, launchMode);
    //        intent.putExtra(PlayConstant.AID, (int) aid);
    //        intent.putExtra(PlayConstant.VID, (int) vid);
    //        intent.putExtra(PlayConstant.FROM, from);
    //        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    //        LetvLogApiTool.createPlayLogInfo("H265VideoClickPlayStart", vid + " aid =" + aid, "-");
    //        if (!(context instanceof Activity)) {
    //            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    //        }
    //        context.startActivity(intent);
    //    }

    /**
     * 打开播放页
     * 播放视频(topic)
     * added by haitian
     */
    public static void launchTopic(Context context, long zid, int type, int from) {
        StatisticsUtils.mClickImageForPlayTime = System.currentTimeMillis();
        int launchMode = 0;
        if (type == AlbumNew.Type.VRS_MANG) {
            launchMode = PlayConstant.LAUNCH_MODE_TOPIC_ALBUM;
        } else {
            launchMode = PlayConstant.LAUNCH_MODE_TOPIC_VIDEO;
        }
        Intent intent = new Intent(context, AlbumPlayActivity.class);
        intent.putExtra(PlayConstant.LAUNCH_MODE, launchMode);
        intent.putExtra(PlayConstant.ZID, zid);
        intent.putExtra(PlayConstant.TYPE, type);
        intent.putExtra(PlayConstant.FROM, from);
        intent.putExtra(PlayConstant.FLOATPAGEINDEX, FLOAT_TOPIC_HALF_NINE);
        intent.putExtra(PlayConstant.IS_FROM_PUSH, LetvSDK.getInstance().isPush() ? true : false);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        LetvLogApiTool.createPlayLogInfo("TopicVideoClickPlayStart", "-" + " zid =" + zid, "-");
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    public static void launchTopic(Context context, long zid, long pid, long pvid, int type,
                                   int from) {
        StatisticsUtils.mClickImageForPlayTime = System.currentTimeMillis();
        int launchMode = 0;
        if (type == AlbumNew.Type.VRS_MANG) {
            launchMode = PlayConstant.LAUNCH_MODE_TOPIC_ALBUM;
        } else {
            launchMode = PlayConstant.LAUNCH_MODE_TOPIC_VIDEO;
        }
        Intent intent = new Intent(context, AlbumPlayActivity.class);
        intent.putExtra(PlayConstant.LAUNCH_MODE, launchMode);
        intent.putExtra(PlayConstant.ZID, zid);
        intent.putExtra(PlayConstant.PID, pid);
        intent.putExtra(PlayConstant.PVID, pvid);
        intent.putExtra(PlayConstant.TYPE, type);
        intent.putExtra(PlayConstant.FROM, from);
        intent.putExtra(PlayConstant.FLOATPAGEINDEX, FLOAT_TOPIC_HALF_NINE);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        LetvLogApiTool.createPlayLogInfo("TopicVideoClickPlayStart",
                pvid + "", "zid =" + zid + " pid=" + pid);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    /**
     * 打开下载进入
     *
     * 播放专辑或者视频
     * */
    //    public static void launchDownload(Context context, long aid, long vid, int from, String pageId) {
    //        StatisticsUtils.mClickImageForPlayTime = System.currentTimeMillis();
    ////        if (from == PlayConstant.LAUNCH_FROM_DOWNLOAD) {
    ////            ActivityUtils.getInstance().removeActivity(AlbumPlayActivity.class.getName(), true);
    ////            LetvApplication.getInstance().setFromHalf(true);
    ////        } else {
    ////            LetvApplication.getInstance().setFromHalf(false);
    ////        }
    //        Intent intent = new Intent(context, AlbumPlayActivity.class);
    //        intent.putExtra(PlayConstant.LAUNCH_MODE, PlayConstant.LAUNCH_MODE_DOWNLOAD);
    //        intent.putExtra(PlayConstant.AID, (int) aid);
    //        intent.putExtra(PlayConstant.VID, (int) vid);
    //        intent.putExtra(PlayConstant.FROM, PlayConstant.LAUNCH_FROM_DOWNLOAD);
    //        intent.putExtra(PlayConstant.PAGE_ID, pageId);
    //        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    //        LetvLogApiTool.createPlayLogInfo("DownLoadVideoClickPlayStart", vid + " aid=" + aid, "-");
    //        if (!(context instanceof Activity)) {
    //            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    //        }
    //        context.startActivity(intent);
    //        AlbumPlayActivity.sIsFromDownload = true;
    //    }

    /**
     * 打开陈跃果的本地播放器
     *
     * @param context
     *         上下文
     * @param filePath
     *         要打开的视频的sd卡路径
     * @param seek
     *         当前已经播放到的位置 单位秒
     */
    public static void launchLocalPlayer(Context context, String filePath, long seek) {
        LocalPlayerActivity.launcher(context, filePath, (int) (seek * 1000));
    }

    /**
     * 为随便看使用，打开跃果的本地播放器时，需要传入title
     *
     * @param context
     *         上下文
     * @param filePath
     *         要打开的视频的sd卡路径
     * @param title
     *         随便看视频的标题
     */
    public static void launchLocalPlayer(Context context, String filePath, String title) {
        LocalPlayerActivity.launcher(context, filePath, title);
    }

    public static void setIsPidIn(Intent intent) {
        int aid = intent.getIntExtra("aid", 0);
        int vid = intent.getIntExtra("vid", 0);
        if (vid <= 0 && aid > 0) {
            LogInfo.log("", "setIsPidIn isPidIn true ");
            isPidIn = true;
        }
    }

    /**
     * 初始化M站跳转参数
     *
     * @param intent
     */
    public static void initMParams(Activity activity, Intent intent) {
        if (intent == null || intent.getData() == null) {
            return;
        }

        Uri data = intent.getData();
        LogInfo.log("sguotao", "M data" + data);

        String scheme = intent.getScheme();
        if (scheme != null && LETVCLIENT.equalsIgnoreCase(scheme)) {// 判断从那里跳转进来
            //            DownloadManager.pauseAllDownload();
        }

        mLetvFunction(activity, data, intent);
    }

    private static void mLetvFunction(Activity activity, Uri data, Intent intent) {
        LogInfo.log("zhuqiao", "M站跳转的init:" + data.toString());
        // actionType:0点播全屏；9点播半瓶
        // leadletvclient://msiteAction?actionType=xx&vid=xx&aid=xxx&cid=xxx&weburl=xxx&streamid=xxx&from＝xxx
        String actionType = data.getQueryParameter("actionType");
        String aid = data.getQueryParameter("aid");
        String vid = data.getQueryParameter("vid");
        String weburl = data.getQueryParameter("weburl");
        String zid = data.getQueryParameter("zid");
        String mtype = data.getQueryParameter("back");// 是否返回源应用
        String cidString = data.getQueryParameter("cid");// 频道id
        String cPageIdString = data.getQueryParameter("pageid");// pageId
        String cTypeString = data.getQueryParameter("ctype");// 频道类型
        String cName = data.getQueryParameter("cname");// 频道名
        String livehalf = data.getQueryParameter("ishalf");// 直播是否是半屏 1:半屏；0：全屏
        String liveType = data.getQueryParameter("livetype");// 直播类型
        String liveIsPay = data.getQueryParameter("ispay");// 直播是否付费 1：付费；0：免费
        String liveId = data.getQueryParameter("liveid");// 直播id
        String utype = data.getQueryParameter("utype");// 启动类型
        String from = data.getQueryParameter("from");// 来源
        String localUrl = data.getQueryParameter("localurl");

        boolean isBack2Source = true;
        try {
            if (mtype != null) {
                isBack2Source = BaseTypeUtils.stoi(mtype, 1) == 1;
            }
        } catch (Exception e) {
            // do nothing
        }

        LogInfo.log("zhuqiao", "isBack2Source:" + isBack2Source);

        if (data != null) {
            if (TextUtils.isEmpty(utype)) {
                utype = "-";
            }
            if (TextUtils.isEmpty(from)) {
                from = "-";
            }
            // 启动类型，播放统计时用
            StatisticsUtils.setStartType(utype);
            StatisticsUtils.setFrom(from);
            // M站启动时，设置播放统计用的ref属性
            StatisticsUtils.setPlayInfoRef(0);
        }

        int type = -1;// 默认值为-1，即内部调用
        int albumId = 0;
        int videoId = 0;
        long zhuantiId = 0;
        int cid = 0;
        int cPageId = 0;
        int cType = 0;
        boolean isHalf = false;
        boolean isPay = false;
        if (!TextUtils.isEmpty(actionType)) {
            type = Integer.parseInt(actionType);
        }
        if (!TextUtils.isEmpty(aid)) {
            albumId = Integer.parseInt(aid);
        }
        if (!TextUtils.isEmpty(vid)) {
            videoId = Integer.parseInt(vid);
        }

        if (!TextUtils.isEmpty(zid)) {
            zhuantiId = BaseTypeUtils.stol(zid);
        }
        if (!TextUtils.isEmpty(cidString)) {
            cid = Integer.parseInt(cidString);
        }
        if (!TextUtils.isEmpty(cPageIdString)) {
            cPageId = Integer.parseInt(cPageIdString);
        }
        if (!TextUtils.isEmpty(cTypeString)) {
            cType = Integer.parseInt(cTypeString);
        }
        try {
            if (!TextUtils.isEmpty(livehalf)) {
                isHalf = Integer.parseInt(livehalf) == 1;
            }
            if (!TextUtils.isEmpty(liveIsPay)) {
                isPay = Integer.parseInt(liveIsPay) == 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        StatisticsUtils.mClickImageForPlayTime = System.currentTimeMillis();
        int launchMode = 0;
        intent.putExtra(PlayConstant.FROM, PlayConstant.LAUNCH_FROM_MSITE);
        intent.putExtra(PlayConstant.BACK, isBack2Source);
        if (type == MLetvJumpProcess.LT_MSITE_ACTION_TYPE_PLAY) {
            // M站全屏播放
            if (albumId > 0 || videoId > 0) {
                launchMode = PlayConstant.LAUNCH_MODE_VIDEO;
                intent.putExtra(PlayConstant.LAUNCH_MODE, launchMode);
                intent.putExtra(PlayConstant.AID, albumId);
                intent.putExtra(PlayConstant.VID, videoId);
            } else if (zhuantiId > 0) {
                launchMode = PlayConstant.LAUNCH_MODE_TOPIC_VIDEO;
                intent.putExtra(PlayConstant.LAUNCH_MODE, launchMode);
                intent.putExtra(PlayConstant.ZID, zhuantiId);
                intent.putExtra(PlayConstant.TYPE, type);
                intent.putExtra(PlayConstant.IS_FROM_PUSH, LetvSDK.getInstance().isPush());
            }
        } else if (type == MLetvJumpProcess.LT_MSITE_ACTION_TYPE_PAY) {
            // 支付
            //            MainLaunchUtils.jump2Pay(activity);
        } else if (type == MLetvJumpProcess.LT_MSITE_ACTION_TYPE_SHARE) {
            // 分享(暂时不做处理)
        } else if (type == MLetvJumpProcess.LT_MSITE_ACTION_TYPE_LIVE) {
            // M站直播
            //            MainLaunchUtils.jump2Live(activity, liveType, isPay, liveId, isHalf);
            activity.finish();
        } else if (type == MLetvJumpProcess.LT_MSITE_ACTION_TYPE_WEBVIEW) {
            // H5页面
            //            MainLaunchUtils.jump2H5(activity, weburl);
        } else if (type == MLetvJumpProcess.LT_MSITE_ACTION_TYPE_SUBJECT) {
            // 跳转至发现
            //            MainLaunchUtils.jump2Subject(activity);
        } else if (type == MLetvJumpProcess.LT_MSITE_ACTION_TYPE_CHANNEL) {
            // 频道
            //            MainLaunchUtils.jump2Channel(activity, cid, cPageId, cName, cType);
        } else if (type == MLetvJumpProcess.LT_MSITE_ACTION_TYPE_USERCENTER) {
            // 个人中心，我的tab
            //            MainLaunchUtils.jump2My(activity);
        } else if (type == MLetvJumpProcess.LT_MSITE_ACTION_TYPE_HOT) {
            // 热点
            //            MainLaunchUtils.jump2Hot(activity);
        } else if (type == MLetvJumpProcess.LT_MSITE_ACTION_TYPE_HALFPLAY) {
            // M站半屏播放
            if (albumId > 0 || videoId > 0) {
                launchMode = PlayConstant.LAUNCH_MODE_ALBUM;
                intent.putExtra(PlayConstant.LAUNCH_MODE, launchMode);
                intent.putExtra(PlayConstant.AID, albumId);
                intent.putExtra(PlayConstant.VID, videoId);
            } else if (zhuantiId > 0) {
                launchMode = PlayConstant.LAUNCH_MODE_TOPIC_ALBUM;
                intent.putExtra(PlayConstant.LAUNCH_MODE, launchMode);
                intent.putExtra(PlayConstant.ZID, zhuantiId);
                intent.putExtra(PlayConstant.TYPE, type);
                intent.putExtra(PlayConstant.IS_FROM_PUSH, LetvSDK.getInstance().isPush());
            }
            // else {
            // UIsUtils.showToast(activity, R.string.param_wrong);
            // }
        } else if (type == MLetvJumpProcess.LT_MSITE_ACTION_TYPE_LOCALPLAY) {
            // 本地播放器
            if (!TextUtils.isEmpty(localUrl)) {
                intent.putExtra(PlayConstant.LAUNCH_MODE, PlayConstant.LAUNCH_MODE_URI);
                intent.putExtra(PlayConstant.URI, localUrl);
                intent.putExtra(PlayConstant.PLAY_MODE, PlayConstant.PLAY_MODE_SYSTEM);
                intent.putExtra(PlayConstant.PAGE_ID, PageIdConstant.localPage);
            } else {
                ToastUtil.toast(R.string.param_wrong);
            }
        } else if (type == MLetvJumpProcess.LT_MSITE_ACTION_TYPE_CONSUMERECORD) {
            jump2ConsumeRecord(activity);
        } else if (type == MLetvJumpProcess.LT_MSITE_ACTION_TYPE_DOWNLOAD_COMPLETE_TAB) {
            // 跳转到缓存页的已完成tab
            jump2Donwloaded(activity);
        } else if (type == MLetvJumpProcess.LT_MSITE_ACTION_TYPE_HOME) {
            // 跳转到home
            //            MainLaunchUtils.jump2Home(activity);
        } else if (type == MLetvJumpProcess.LT_MSITE_ACTION_TYPE_PLAY_RECORD) {
            // 跳转到播放记录
            jump2PlayRecord(activity);
        } else if (type == MLetvJumpProcess.LT_MSITE_ACTION_TYPE_FAVORITE) {
            // 跳转到收藏
            jump2PlayFavorite(activity);
        } else {
            // wxf 文件夹打开视频会走这里，如果走到下面setData(null)后。视频路径将无法取出，所以return;
            return;
        }
        intent.setData(null);
        LetvLogApiTool.createPlayLogInfo("VideoClickPlayStart", vid + " aid =" + aid, "-");
    }

    private static void jump2ConsumeRecord(Activity activity) {
        if (activity != null) {
            //            ConsumeRecordActivity.launch(activity);
            activity.finish();
        }
    }

    private static void jump2Donwloaded(Activity activity) {
        if (activity != null) {
            Intent i = new Intent();
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            //            i.setClass(activity, MyDownloadActivity.class);
            i.putExtra("fromPush", true);
            i.putExtra("toHomePage", true);
            i.putExtra("toDownload", true);
            activity.startActivity(i);
            activity.finish();
        }
    }

    private static void jump2PlayRecord(Activity activity) {
        if (activity != null) {
            //            activity.startActivity(new Intent(activity, MyPlayRecordActivity.class));
            activity.finish();
        }
    }

    private static void jump2PlayFavorite(Activity activity) {
        if (activity != null) {
            //            activity.startActivity(new Intent(activity, MyCollectActivity.class));
            activity.finish();
        }
    }
}
